const express = require('express')
const router = express.Router()
const axios = require('axios');


router.post('/wxGetOpenid', (req, res) => {
    try {
        const appid = "wx6c91840649f6e23d"
        const secret = '37780d35a7c5bfcb2c77822d644a4272';
        const {js_code} = req.body;
        // const js_code = "0a1EbcHa1reC1I0ecAGa1hO17N3EbcHN";
        const grant_type = 'authorization_code';

        const url = `https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${js_code}&grant_type=${grant_type}`;
        axios.get(url).then(response => {
            // console.log(response);
            res.json({
                status: 200,
                message: '获取openid成功',
                data: response.data
            })
        }).catch(e => {
            res.json({
                status: 500,
                message: '获取openid失败',
                data: e
            })
        })
    } catch (error) {
        // 在这里处理错误
        console.error(error.message);
        res.status(400).json({ code: 400, message: error.message });
    }
})

module.exports = router